package bandidong.model;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;

@Entity
@Table(name = "tbl_mobile", catalog = "web_ban_di_dong_db", schema = "", uniqueConstraints = {
    @UniqueConstraint(columnNames = {"mobile_name"})})
@XmlRootElement
public class Mobile implements Serializable {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "mobile_id", nullable = false)
    private Integer mobileId;
    
    @NotNull
    @Size(min = 1, max = 200)
    @Column(name = "mobile_name", nullable = false, length = 200)
    private String mobileName;
    
    @Size(max = 1000)
    @Column(name = "mobile_description", length = 1000)
    private String mobileDescription;
    
    @NotNull
    @Column(name = "mobile_price", nullable = false)
    private double mobilePrice;
    
    @Size(max = 300)
    @Column(name = "mobile_image", length = 300)
    private String mobileImage;
    
    @Column(name = "release_date")
    @Temporal(TemporalType.DATE)
    private Date releaseDate;
    
    @NotNull
    @Column(nullable = false)
    private int amount;
    
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "mobile", fetch = FetchType.EAGER)
    private Set<OrderDetails> orderDetailsSet = new HashSet<OrderDetails>();
    
    @JoinColumn(name = "category_id", referencedColumnName = "category_id")
    @ManyToOne(fetch = FetchType.EAGER)
    private Category category;
    
    @JoinColumn(name = "company_id", referencedColumnName = "company_id", nullable = false)
    @ManyToOne(optional = false, fetch = FetchType.EAGER)
    private Company company;

    public Mobile() {
    }

    public Mobile(Integer mobileId, String mobileName, String mobileDescription, double mobilePrice, 
            String mobileImage, Date releaseDate, int amount) {
        this.mobileId = mobileId;
        this.mobileName = mobileName;
        this.mobileDescription = mobileDescription;
        this.mobilePrice = mobilePrice;
        this.mobileImage = mobileImage;
        this.releaseDate = releaseDate;
        this.amount = amount;
    }
    
    public Mobile(Integer mobileId, String mobileName, String mobileDescription, double mobilePrice, 
            String mobileImage, Date releaseDate, int amount, Set<OrderDetails> orderDetailsSet, 
            Category category, Company company) {
        this(mobileId, mobileName, mobileDescription, mobilePrice, mobileImage, releaseDate, amount);
        this.orderDetailsSet = orderDetailsSet;
        this.category = category;
        this.company = company;
    }
    
    public Integer getMobileId() {
        return mobileId;
    }

    public void setMobileId(Integer mobileId) {
        this.mobileId = mobileId;
    }

    public String getMobileName() {
        return mobileName;
    }

    public void setMobileName(String mobileName) {
        this.mobileName = mobileName;
    }

    public String getMobileDescription() {
        return mobileDescription;
    }

    public void setMobileDescription(String mobileDescription) {
        this.mobileDescription = mobileDescription;
    }

    public double getMobilePrice() {
        return mobilePrice;
    }

    public void setMobilePrice(double mobilePrice) {
        this.mobilePrice = mobilePrice;
    }

    public String getMobileImage() {
        return mobileImage;
    }

    public void setMobileImage(String mobileImage) {
        this.mobileImage = mobileImage;
    }

    public Date getReleaseDate() {
        return releaseDate;
    }

    public void setReleaseDate(Date releaseDate) {
        this.releaseDate = releaseDate;
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    public Set<OrderDetails> getOrderDetailsSet() {
        return orderDetailsSet;
    }

    public void setOrderDetailsSet(Set<OrderDetails> orderDetailsSet) {
        this.orderDetailsSet = orderDetailsSet;
    }
    
    @JoinColumn(name = "category_id", referencedColumnName = "category_id")
    @ManyToOne(fetch = FetchType.EAGER)
    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }
    
    @JoinColumn(name = "company_id", referencedColumnName = "company_id", nullable = false)
    @ManyToOne(optional = false, fetch = FetchType.EAGER)
    public Company getCompany() {
        return company;
    }

    public void setCompany(Company company) {
        this.company = company;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (mobileId != null ? mobileId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Mobile)) {
            return false;
        }
        Mobile other = (Mobile) object;
        if ((this.mobileId == null && other.mobileId != null) || (this.mobileId != null && !this.mobileId.equals(other.mobileId))) {
            return false;
        }
        return true;
    }
}
